home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 3_0 / MTN_RENA / MTN_RENA.C next >
C/C++ Source or Header  |  1990-12-06  |  6KB  |  206 lines

  1. /* what this thing does it rename all those old tech notes from the format
  2.    '000 about technotes' to the new way, 'TN.000.about technotes'. */
  3.  
  4. #include <HFS.h>
  5. #include <MacTypes.h>
  6. #include <StdFilePkg.h>
  7. #include <ToolboxUtil.h>
  8. #include <EventMgr.h>
  9.  
  10. main()
  11. {
  12.     HParamBlockRec    theFile;
  13.                                                         /* for PBHGetFInfo */
  14.     int        ioVRefNum = 0,                                /* VRefNum (Working Dir) */
  15.             ioFDirIndex = 1;                            /* search index for PBH */
  16.     long    ioDirID = 0;                                /* No DirID! */
  17.     Str255    ioNamePtr,                                    /* Name of file */
  18.             oldName;                                    /* Original name of file */
  19.     
  20.     OSErr    theResult;                                        /* result code */
  21.     
  22.     Boolean    CheckName();
  23.     
  24.     int        numFiles,                                    /* number of files in selected directory */
  25.             i;                                            /* counter */
  26.  
  27.     CursHandle    theCursor;
  28.  
  29.     InitMac();
  30.     
  31.     GetWD(&ioVRefNum);                                    /* get working directory */
  32.     theCursor = GetCursor(watchCursor);                    /* get the watch cursor */
  33.     SetCursor(*theCursor);                                /* set it */
  34.     
  35.     if (ioVRefNum != 0)                                    /* if the user selected a directory */
  36.         {                                                /* setup Parm Block */
  37.         theFile.fileParam.ioCompletion = 0L;
  38.         theFile.fileParam.ioVRefNum = ioVRefNum;
  39.         theFile.fileParam.ioFDirIndex = 1;
  40.         theFile.fileParam.ioDirID = ioDirID;
  41.         theFile.fileParam.ioNamePtr = ioNamePtr;
  42.                                                         /* count the number of files */
  43.         while ((PBHGetFInfo(&theFile, false) == 0))
  44.             {
  45.             numFiles = theFile.fileParam.ioFDirIndex++;
  46.             theFile.fileParam.ioDirID = 0;                /* have to keep zero'ing this! */
  47.             theFile.fileParam.ioNamePtr[0] = 0;            /* kill of the name, too */
  48.             }
  49.  
  50.         theFile.fileParam.ioFDirIndex = 1;                /* set index */
  51.         theFile.fileParam.ioDirID = 0;                    /* have to keep zero'ing this! */
  52.         theFile.fileParam.ioNamePtr[0] = 0;                /* kill of the name, too */
  53.  
  54.         for (i = 1; i<=numFiles; i++)
  55.             {
  56.             if (PBHGetFInfo(&theFile, false) == 0)        /* while there's a file */
  57.                 {
  58.                 AppendString(oldName, 1, ioNamePtr, 1);    /* save the old fileName */
  59.                 if (CheckName(ioNamePtr))                /* see if it's an old technote, format 'nnn' */
  60.                     {                                    /* if it is, return the new name, format 'TN.nnn.' */
  61.                     if (ioNamePtr[0] > 31)                /* if longer than 31 chars */
  62.                         {
  63.                         ioNamePtr[0] = 31;                /* trunc it */
  64.                         }
  65.                                                         /* rename the file */
  66.                     theResult = Rename(oldName, theFile.fileParam.ioVRefNum, ioNamePtr);
  67.                     if (theResult != 0)
  68.                         {
  69.                         ++theFile.fileParam.ioFDirIndex;
  70.                         }
  71.                     }
  72.                 else
  73.                     {
  74.                     ++theFile.fileParam.ioFDirIndex;
  75.                     }
  76.                 }
  77.             theFile.fileParam.ioNamePtr[0] = 0;        /* kill the name (again!) */
  78.             theFile.fileParam.ioDirID = 0;            /* the DirID too! */
  79.             }
  80.         }
  81.         SetCursor(&arrow);                                /* reset the cursor */
  82. }
  83.  
  84. /* gets a working directory from the user.*/
  85.  
  86. GetWD(ioVRefNum)
  87. int        *ioVRefNum;
  88. {
  89.     SFReply        reply;                                    /* reply record */
  90.     Point        where;                                    /* loc of dialog */
  91.     SFTypeList    typeList;                                /* not used */
  92.     
  93.     where.v = 50;                                        /* any point */
  94.     where.h = 50;
  95.     
  96.     SFGetFile(where, 0L, 0L, -1, typeList, 0L, &reply);    /* put up SFGet */
  97.     if (reply.good == false)                            /* if cancel was hit */
  98.         {
  99.         *ioVRefNum = 0;
  100.         }
  101.     else                                                /* selected! */
  102.         {
  103.         *ioVRefNum = reply.vRefNum;
  104.         }
  105.     return;
  106. }
  107.  
  108. /* useless error handler. Basicaly waits until there are 50 errors and
  109.    exits. */
  110. HandleError(theResult)
  111. OSErr    theResult;
  112. {
  113. static int    counter = 0;
  114.  
  115. if (++counter>50)
  116.     {
  117.     ExitToShell();
  118.     }
  119. }
  120.  
  121. /* checks theString to see if it is in the old format. Could be modified
  122.    to check anything, really. */
  123.    
  124. Boolean CheckName(theString)
  125. Str255    theString;
  126. {
  127.     Str255    theCopy, thePart;
  128.     int        index = 1;
  129.     Boolean    numbers;
  130.     
  131.     theCopy[0] = 0;                                        /* zero strings */
  132.     thePart[0] = 0;
  133.     
  134.     while (index <= 3)                                    /* length of header */
  135.         {
  136.         if ((theString[index] >= '0') && (theString[index] <= '9'))
  137.             {
  138.             thePart[index] = theString[index];            /* copy numbers */
  139.             thePart[0] = index;                            /* set length */
  140.             numbers = true;
  141.             }
  142.         else
  143.             {
  144.             numbers = false;                            /* nope */
  145.             break;
  146.             }
  147.         index++;
  148.         }
  149.     if (numbers)                                        /* if it is a tn */
  150.         {                                                /* create the new string */
  151.         AppendString(theCopy, 1, "\pTN.", 1);
  152.         AppendString(theCopy, 4, thePart, 1);
  153.         AppendString(theCopy, 7, "\p.", 1);
  154.         AppendString(theCopy, 8, theString, 5);
  155.         AppendString(theString, 1, theCopy, 1);            /* copy the copy into the original */
  156.         return true;
  157.         }
  158.     return false;
  159. }
  160.  
  161. /* a useful string utility. It will copy a string into another, or copy
  162.    a part of a string into another one. Useful for building strings.
  163.    It will not, however, allow you to jam a string into the middle of
  164.    another a la munger.
  165.  
  166.     theOriginal must be a Str255!
  167.     theCopy can be a non-variable ie: "\ptheCopy".
  168.     start is where to put theCopy in theOriginal.
  169.     start2 is where to start copying in theCopy.
  170.     
  171.     An example from above:                            theCopy
  172.     AppendString(theCopy, 1, "\pTN.", 1);            "\pTN."
  173.     AppendString(theCopy, 4, thePart, 1);            "\pTN.000"
  174.     AppendString(theCopy, 7, "\p.", 1);                "\pTN.000."
  175.     AppendString(theCopy, 8, theString, 5);            "\pTN.000.Aboutâ•”"
  176. */
  177.  
  178. AppendString(theOriginal, start, theCopy, start2)
  179. Str255    theOriginal,
  180.         theCopy;
  181. int        start,
  182.         start2;
  183.  
  184. {
  185.     while ((start2 <= theCopy[0]) && (start <=256))    /* make sure that:
  186.                                                        start2 is in the valid part of the string,
  187.                                                        start doesn't go past the length of 256 */
  188.         {
  189.         theOriginal[start++] = theCopy[start2++];
  190.         }
  191.     theOriginal[0] = start-1;                        /* set length byte */
  192. }
  193.  
  194.  
  195. InitMac()
  196.     {
  197.     InitGraf(&thePort);
  198.     InitFonts();
  199.     FlushEvents(everyEvent, 0);
  200.     InitWindows();
  201.     InitMenus();
  202.     TEInit();
  203.     InitDialogs(0L);
  204.     InitCursor();
  205.     }
  206.